#include "IOStream.h"

using namespace neiderra::core;

bool iStringStream::open(const char* str)
{
	_str = str;
	_pos = 0;
	return true;
}

int iStringStream::seek(long n, SeekType type)
{
	if(n >= size()) return -1;	//eos

	switch(type)
	{
	case stBegin:
		_pos = n;
		break;
	case stCurrent:
		if(n+_pos >= size()) return -1; // eos

		_pos += n;
		break;
	case stEnd:
		_pos = size()-1-n;
		break;
	}
	return _pos;
}

long iStringStream::pos()
{
	return _pos;
}

int iStringStream::read(char* buf, int n)
{
	if(n + _pos >=size()) return -1;
 
	int i;
	for( i=0;i<n;i++)
	{
		buf[i] = _str[_pos+i];
	}
	buf[i] = 0;

	_pos += n;
	return n;
}

int iStringStream::size()
{
	return _str.size();
}
